const express = require("express");
const path = require("path");
const usersModel = require("./models");
let app = express();

app.get("/", (req, res) => {
  res.sendFile(path.join(__dirname, "./views/index.html"));
})

// 路由地址，获取数据库里的用户信息
app.get("/getusers", async (req, res) => {
  // 接收前端传的age参数，根据age参数获取不同的数据
  let { age, gender } = req.query;
  let where; // 查询条件的对象,不给值默认是undefined
  if (age == 0) {
    where = { age: 1 }
  } else if (age == 1) {
    where = { age: -1 }
  }
  let genderWhere // 筛选性别的条件，默认是undefined
  if (gender == 0) {
    genderWhere = { gender: "男" }
  } else if (gender == 1) {
    genderWhere = { gender: "女" }
  }

  let usersData = await usersModel.find(genderWhere).sort(where);
  res.json(usersData);
})

app.listen(8089);